package com.yan_jiu_sheng.LeetCodeHot100.AC;

/**
 * https://leetcode.cn/problems/sort-list/description/?envType=study-plan-v2&envId=top-100-liked
 * 通过
 *
 * @author yulongTian
 * @create 2024-05-31 16:00
 */
public class Test34 {
    public static void main(String[] args) {
        ListNode head4 = new Test34().new ListNode(4);
        ListNode head2 = new Test34().new ListNode(2);
        ListNode head1 = new Test34().new ListNode(1);
        ListNode head3 = new Test34().new ListNode(3);
        head4.next = head2;
        head2.next = head1;
        head1.next = head3;
        ListNode listNode = new Test34().sortList(head4);
        while (listNode != null) {
            System.out.println(listNode.val);
            listNode = listNode.next;
        }
    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }


    private ListNode sortList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode last = head;
        int len = 1;
        while (last.next != null) {
            len++;
            last = last.next;
        }

        return mergeSort(head, head, last, len);
    }

    private ListNode mergeSort(ListNode head, ListNode left, ListNode right, int len) {
        if (left.hashCode() == right.hashCode())
            return left;
        int midIndex = len / 2;
        ListNode mid = head;
        while (midIndex > 1) {
            mid = mid.next;
            midIndex--;
        }


        ListNode leftNodes = mergeSort(left, left, mid, len / 2); //左有序数组
        ListNode rightNodes = mergeSort(mid.next, mid.next, right, len - len / 2); //右有序数组

        int i = 0, j = 0;
        ListNode newHead = null;
        ListNode pleft = null;//i
        ListNode pright = null;//j
        if (leftNodes.val <= rightNodes.val) {
            i = 1;
            newHead = new ListNode(leftNodes.val);
            pleft = leftNodes.next;
            pright = rightNodes;
        } else {
            j = 1;
            newHead = new ListNode(rightNodes.val);
            pright = rightNodes.next;
            pleft = leftNodes;
        }
        ListNode p = newHead;//m
        while (i < (len / 2) && j < (len - len / 2)) {
            if (pleft.val < pright.val) {
                ListNode temp = new ListNode(pleft.val);
                pleft = pleft.next;
                p.next = temp;
                p = temp;
                i++;
            } else {
                ListNode temp = new ListNode(pright.val);
                pright = pright.next;
                p.next = temp;
                p = temp;
                j++;
            }
        }

        while (i < len / 2) {
            ListNode temp = new ListNode(pleft.val);
            pleft = pleft.next;
            p.next = temp;
            p = temp;
            i++;
        }
        while (j < len - len / 2) {
            ListNode temp = new ListNode(pright.val);
            pright = pright.next;
            p.next = temp;
            p = temp;
            j++;
        }
        return newHead;
    }

}
